package data;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class DepartmentData {
	private int mId = 0;
	private String mName = null;
	private String mNameShort = null;
	private String mBoss = null;
	private String mAddress1 = null;
	private String mAddress2 = null;
	private String mContact = null;
	private double mNorm = 0.0;

	public DepartmentData(int id, String name, String nameShort, String boss, String address1, String address2,
			String contact, double norm) {
		mId = id;
		mName = name;
		mNameShort = nameShort;
		mBoss = boss;
		mAddress1 = address1;
		mAddress2 = address2;
		mContact = contact;
		mNorm = norm;
	}

	public DepartmentData(int id) {
		mId = id;
		mName = null;
		mNameShort = null;
		mBoss = null;
		mAddress1 = null;
		mAddress2 = null;
		mContact = null;
		mNorm = 0;
	}
	
	public int getId() {
		return mId;
	}

	public String getName() {
		return mName;
	}

	public String getNameShort() {
		return mNameShort == null || mNameShort.equals("") ? mName : mNameShort;
	}

	public String getBoss() {
		return mBoss;
	}

	public String getAddress1() {
		return mAddress1;
	}

	public String getAddress2() {
		return mAddress2;
	}

	public String getContact() {
		return mContact;
	}

	public double getNorm() {
		return mNorm;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof DepartmentData))
			return false;
		
		return mId == ((DepartmentData)obj).getId();
	}

	public final static JsonDeserializer<DepartmentData> deserializer = new JsonDeserializer<DepartmentData>() {

		@Override
		public DepartmentData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {

			int id = json.getAsJsonObject().get("id").getAsInt();
			String name = json.getAsJsonObject().get("name").getAsString();
			String shortName = json.getAsJsonObject().get("name_short").getAsString();
			String boss = json.getAsJsonObject().get("boss").getAsString();
			String address1 = json.getAsJsonObject().get("address1").getAsString();
			String address2 = json.getAsJsonObject().get("address2").getAsString();
			String contact = json.getAsJsonObject().get("contact").getAsString();
			double norm = json.getAsJsonObject().get("norm").getAsDouble();
			return new DepartmentData(id, name, shortName, boss, address1, address2, contact, norm);
		}
	};
	
	public final static JsonSerializer<DepartmentData> serializer = new JsonSerializer<DepartmentData>() {

		@Override
		public JsonElement serialize(DepartmentData arg0, Type arg1, JsonSerializationContext arg2) {
			return new JsonPrimitive(arg0.getId());
		}
	};
}
